package modele;

import bdd.DAOJoueur;
import bdd.DAOPhrase;
import java.sql.SQLException;
import java.util.Stack;
import java.util.logging.Level;
import java.util.logging.Logger;
import vue.ThreadBarre;
import vue.VueJeu1;

/**
 *Cette classe est le coeur du fonctionnement du jeu1
 * @author Shaance
 */
public class Jeu1 extends Jeu {

    private boolean isFinished;
    private Stack<ChaineDeCaracteres> phrases;
    private vue.VueJeu1 vue;
    private Chrono1 chrono;
    private ThreadBarre tb;
    
    /**
     *
     * @param duree
     * @throws java.sql.SQLException
     */
    public Jeu1(int duree) throws SQLException {

        super(duree);
        this.isFinished = false;
        chrono = new Chrono1(this);
        tb=new ThreadBarre(this);
        
        DAOPhrase dp = new DAOPhrase();

        if (duree == 30) {
            phrases = dp.getPhrases(20);
        } else {
            phrases = dp.getPhrases(40);
        }

        this.vue = new VueJeu1();

    }

    public VueJeu1 getVue() {
        return vue;
    }

    public void setVue(VueJeu1 vue) {
        this.vue = vue;
    }

    @Override
    public void run() {


        this.chrono.start();
        this.tb.start();
        
        this.setScore(0);

        while (!phrases.empty() && this.chrono.getTimer() > 1) {
            vue.setLastInput(null);
            this.vue.afficherPhrase(this.phrases.peek().getContenu());

            while (this.chrono.getTimer() > 0 && this.vue.getLastInput() == null) {
                System.out.print("");
            }
            if (vue.getLastInput() != null) {
                if (vue.getLastInput().equals(phrases.pop().getValeur())) {

                    setStreak(getStreak() + 1);
                    
                    if (getStreak() > 2 && getStreak() < 6) {
                        setCombo(1.2);
                    } else if (getStreak()>5 && getStreak() < 10) {
                        setCombo(1.5);
                    } else if(getStreak()>9){
                        setCombo(2);
                    }
                    setScore(getScore() + 100 * getCombo());

                } else {
                    setStreak(0);
                    setCombo(1);
                    if(getScore()-100 >=0){
                        setScore(getScore()-100);
                    }else{
                        setScore(0);
                    }
                }
            }

            this.vue.afficherScore(getScore());
            this.vue.afficherCombo(String.valueOf(getCombo()));
        }
        
        setScore(getScore()+getChrono().getTimer()*50); //bonus temps
     
        this.vue.afficherRejouer();
        this.vue.afficherMenu();
        String pseudo=this.vue.saisirPseudo(getScore());
        try {
            this.enregistrerPseudo(pseudo);
        } catch (SQLException ex) {
            Logger.getLogger(Jeu1.class.getName()).log(Level.SEVERE, null, ex);
        }
       
       

    }
    
    public void enregistrerPseudo(String pseudo) throws SQLException{
        JoueurEnglishNotNull j = new JoueurEnglishNotNull();
        j.setUsername(pseudo);
        j.setHighScore((int) this.getScore());
        DAOJoueur dj = new DAOJoueur();
        dj.insertScore(j, this.getDuree());
    }

    @Override
    public void pause() {
        // TODO - implement Jeu1.pause
        throw new UnsupportedOperationException();
    }

    public boolean inputUtilisateur() {
        // TODO - implement Jeu1.inputUtilisateur
        throw new UnsupportedOperationException();
    }

    public void prochainePhrase() {
        // TODO - implement Jeu1.prochainePhrase
        throw new UnsupportedOperationException();
    }

    public void calculerScore() {
        // TODO - implement Jeu1.calculerScore
        throw new UnsupportedOperationException();
    }

    public Stack<ChaineDeCaracteres> getPhrases() {
        return phrases;
    }

    public boolean getIsFinished() {
        return chrono.getTimer() == 0 || this.phrases.size()==0;
    }

    public void setIsFinished(boolean isFinished) {
        this.isFinished = isFinished;
    }

    public Chrono1 getChrono() {
        return chrono;
    }

    public void setChrono(Chrono1 chrono) {
        this.chrono = chrono;
    }
}
